﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyTiemVang.DAO;
using QuanLyTiemVang.BUS;
using System.Text.RegularExpressions;
using QuanLyTiemVang.Classes;
namespace QuanLyTiemVang.GUI
{    
    public partial class DangKyTaiKhoan : Window, IPhanQuyenHelper
    {
        public DangKyTaiKhoan()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
            this.txtTenDangNhap.Focus();
            this.comboLoaiTaiKhoan.ItemsSource = TaiKhoanDAO.LoaiTaiKhoans();
        }

        private void btnThoat_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnDangKy_Click_1(object sender, RoutedEventArgs e)
        {
            // check information
            if (checkValid() == true)
            {
                // create data transfer object
                TAIKHOAN tk = new TAIKHOAN();
                tk.Email = this.txtEmail.Text;
                tk.MatKhau = this.txtMatKhau.Password;
                tk.TenDangNhap = this.txtTenDangNhap.Text;
                LOAITAIKHOAN ltk = (LOAITAIKHOAN)comboLoaiTaiKhoan.SelectedItem;
                if(ltk.MaLoaiTaiKhoan==1 // admin
                    && CurrentUser.Instance.PhanQuyen != TypePhanQuyen.Admin) // not admin
                {
                    MessageBox.Show("Chỉ có admin mới có quyền thêm admin!");
                    return;
                }
                tk.MaLoaiTaiKhoan = ltk.MaLoaiTaiKhoan;

                // check email
                if (TaiKhoanBUS.KTraEmail(tk.Email) == false)
                {
                    MessageBox.Show("Email đã tồn tại trong Database!");
                    this.txtEmail.Focus();
                    this.txtEmail.SelectAll();
                    return;
                }

                // save to database
                try
                {                    
                    // save to database
                    TaiKhoanBUS.ThemTaiKhoan(tk);
                    MessageBox.Show("Thêm thành công!");
                    this.clearControl();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }            
        }

        private void btnThemMoi_Click_1(object sender, RoutedEventArgs e)
        {
            this.clearControl();
        }

        // check valid control
        private bool checkValid()
        {
            //empty field
            if(string.IsNullOrEmpty(txtEmail.Text)==true
                ||string.IsNullOrEmpty(txtMatKhau.Password)==true
                ||string.IsNullOrEmpty(txtTenDangNhap.Text)==true
                || string.IsNullOrEmpty(txtXacNhanMatKhau.Password) == true)
            {
                MessageBox.Show("Điền đầy đủ thông tin!");
                return false;
            }
            //password does not match
            else if (txtMatKhau.Password != txtXacNhanMatKhau.Password)
            {
                MessageBox.Show("Password không trùng nhau!");
                return false;
            }
            // is email valid?
            else if (emailIsValid(this.txtEmail.Text) == false)
            {
                MessageBox.Show("Email không hợp lệ!");
                return false;
            }
            else if (txtTenDangNhap.Text.Length < 8 )
            {
                MessageBox.Show("Tên đăng nhập phải dài hơn 8 kí tự!");
                return false;
            }
            else if (txtMatKhau.Password.Length < 8)
            {
                MessageBox.Show("Mật khẩu phải dài hơn 8 kí tự!");
                return false;
            }
            return true;            
        }

        // clear controls
        private void clearControl()
        {
            this.txtEmail.Clear();
            this.txtMatKhau.Clear();
            this.txtTenDangNhap.Clear();
            this.txtXacNhanMatKhau.Clear();
            this.txtTenDangNhap.Focus();
            this.comboLoaiTaiKhoan.SelectedIndex = -1;
        }

        public bool emailIsValid(string email)
        {
            string expresion;
            expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
            if (Regex.IsMatch(email, expresion))
            {
                if (Regex.Replace(email, expresion, string.Empty).Length == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }

        public void SetAdminFunction()
        {
            
        }

        public void SetStaffFunction()
        {
            
        }

        public void SetCustomerFunction()
        {
            
        }

        public void SetNonFunction()
        {
            
        }
    }
}
